﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Salon
{
    public partial class FormUser : Form
    {
        public User User { get; set; }
        
        public FormUser()
        {
            InitializeComponent();
            
        }

        //konstruktor inicjujący formatkę danymi konkretnego usera
        public FormUser(User user,bool current)
        {
            InitializeComponent();
            if (user != null)
            {            
                fillFields(user);

            }
            if (current)
            {
                btnNew.Enabled = false;
                Text = Forms.USER_MY_DATA;
            }
            else
            {
                btnNew.Enabled = true;
                Text = Forms.USER_FORM;
            }
        } 

        private void fillFields(User user)
        {
            this.User = user;
            txtNAme.Text = user.Name;
            txtSurname.Text = user.SurName;
            cmbCountry.Text = user.Country;//w przyszlosci zmienic na SelecteText
            dtpBirthDate.Value = user.BirthDate.Date;
            txtLogin.Text = user.Login;
            txtId.Text = user.Id.ToString();
            txtPassword.Text = "";
            txtPassword2.Text = "";
            txtCity.Text= user.City;
            txtStreet.Text = user.Street;
            txtZip.Text = user.Zip;
            txtFlatNumber.Text = user.FlatNumber;
            
            fillRoles(user.Role);
        }

        private void fillRoles(int role)
        {
            chkServisman.Checked = (role & (int)Roles.ServiceMan) > 0 ? true : false;
            chkSalesman.Checked = (role & (int)Roles.Salesman) > 0 ? true : false;
            chkManager.Checked = (role & (int)Roles.Manager) > 0 ? true : false;
            chkAdministrator.Checked = (role & (int)Roles.Administrator) > 0 ? true : false;
            chkSuperAdmin.Checked = (role & (int)Roles.SuperAdministrator) > 0 ? true : false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private String validateFields()
        {
            String fields = "";
            fields += txtLogin.Text.Equals(String.Empty) ? "login, " : "";
            fields += txtNAme.Text.Equals(String.Empty) ? "imię, " : "";
            fields += txtSurname.Text.Equals(String.Empty) ? "nazwisko, " : "";
            fields += cmbCountry.Text.Equals(String.Empty) ? "państwo, " : "";//pozniej zmienic na SelectedText
            fields += txtCity.Text.Equals(String.Empty) ? "miasto, " : "";
            fields += txtStreet.Text.Equals(String.Empty) ? "ulica, " : "";
            fields += txtZip.Text.Equals(String.Empty) ? "kod, " : "";
            fields += txtFlatNumber.Text.Equals(String.Empty) ? "numer, " : "";
            fields += cmbSalon.Text.Equals(String.Empty) ? "salon, " : "";

            if (!chkAdministrator.Checked && !chkManager.Checked && !chkSalesman.Checked && !chkServisman.Checked && !chkSuperAdmin.Checked)
            {
                fields += "role, ";
            }

            return fields;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            String fields = validateFields();
            if (!fields.Equals(String.Empty))
            {
                MessageBox.Show("Proszę uzupełnić pola " + fields.Substring(0,fields.Length-2)+".");
                return;
            }
            
            
            if (this.User==null && ServiceFactory.getUserService().checkIfLoginExists(txtLogin.Text))
            {
                if (!Global.currentUser.Login.Equals(txtLogin.Text))
                    MessageBox.Show("Podany login już istnieje. Proszę wybrać inny.");
                return;
            }
            
            //przy zapisywaniu najpierw tworzymy obiekt modelu a potem przekazujemy do odpowiedniego serwisu
            User user = this.User==null ? new User() : this.User;
            user.Name = txtNAme.Text;
            user.SurName = txtSurname.Text;
            user.Country = cmbCountry.Text;//zmienic w przyszlosci
            user.BirthDate = dtpBirthDate.Value;
            user.Role = getRole();
            user.Active = true;
            user.Salon = new AutoSalon(); //tymczasowa zaślepka na salon dodany ręcznie do bazy żeby klucze nie protestowały
            user.Login = txtLogin.Text;
            user.Password = Global.EncodePassword(txtPassword.Text);
            user.City = txtCity.Text;
            user.Street = txtStreet.Text;
            user.Zip = txtZip.Text;
            user.FlatNumber = txtFlatNumber.Text;

            ServiceFactory.getUserService().saveUser(user);

            MessageBox.Show("Dodano użytkownika " + user.Name);

        }

        private int getRole()
        {
            int role = 0;
            role += chkServisman.Checked ? (int)Roles.ServiceMan :0;
            role += chkSalesman.Checked ? (int)Roles.Salesman : 0;
            role += chkManager.Checked ? (int)Roles.Manager : 0;
            role += chkAdministrator.Checked ? (int)Roles.Administrator : 0;
            role += chkSuperAdmin.Checked ? (int)Roles.SuperAdministrator : 0;

            return role;
        }

        private void FormUser_Load(object sender, EventArgs e)
        {
            dtpBirthDate.Value = DateTime.Now;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            this.User = null;
            this.txtCity.Text = "";
            this.txtFlatNumber.Text = "";
            this.txtId.Text = "";
            this.txtLogin.Text = "";
            this.txtNAme.Text = "";
            this.txtPassword.Text = "";
            this.txtPassword2.Text = "";
            this.txtStreet.Text = "";
            this.txtSurname.Text = "";
            this.txtZip.Text = "";
            this.cmbCountry.SelectedText = "";
            this.cmbSalon.SelectedText = "";
            this.chkAdministrator.Checked = false;
            this.chkManager.Checked = false;
            this.chkSalesman.Checked = false;
            this.chkServisman.Checked = false;
            this.chkSuperAdmin.Checked = false;
            dtpBirthDate.Value = DateTime.Now;
        }

       
    }
}
